/**
 * 
 */

package org.spring.cloud.pilot.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.cloud.pilot.Restarter;
import org.spring.cloud.pilot.event.ClassPathChangedEvent;
import org.spring.cloud.pilot.jar.ClassPathFileSystemWatcher;
import org.spring.cloud.pilot.listener.PromoteChangeEvent;
import org.spring.cloud.pilot.listener.SecondListener;
import org.spring.cloud.pilot.listener.SpringListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

/**
 * 
 * ClassName: RestartAutoConfiguration <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2019年10月25日 下午5:13:31 <br/>
 *
 * @author admin
 * @version
 * @since JDK 1.8
 */
@Configuration
@ConditionalOnProperty(value = "management.dev.enabled", havingValue = "true", matchIfMissing = false)
public class RestartAutoConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(RestartAutoConfiguration.class);

 

  

    @Bean
    @ConditionalOnBean(ApplicationEventPublisher.class)
    public ServletListenerRegistrationBean<SecondListener>
        listenerRegistration(ApplicationEventPublisher eventPublisher) {
        ServletListenerRegistrationBean<SecondListener> bean =
            new ServletListenerRegistrationBean<>(new SecondListener(eventPublisher));
        return bean;
    }

    @Bean
    public SpringListener listener() {
        SpringListener bean = new SpringListener();
        return bean;
    }

    @Bean
    @ConditionalOnMissingBean
    public ClassPathFileSystemWatcher classPathFileSystemWatcher() {
        ClassPathFileSystemWatcher watcher = new ClassPathFileSystemWatcher();
        // watcher.setStopWatcherOnRestart(true);
        return watcher;
    }
    
    @EventListener
    public void envListener(PromoteChangeEvent event) {
        LOGGER.info("PromoteChangeEvent改33变----");
        Restarter.getInstance().reboot();
    }

}
